// pages/juanxq/juanxq.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    daijin: '',//所有数据
    USER_COUPON_PHONE: '',//电话号码
    VAILD: '',//获奖判定
    NEW_VAILD: '',
    USER_COUPON_ID: '',
    COUPON_IMG: '',
    COUPON_BIG_IMG: '',
    openid: '',
    login_id: '',//支付宝账号

    showView: false,
    flag: true,//节流
  },



  receive_a() {
    // this.setData({
    //   showView: true,
    // })
 
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,

    })
    this.tjok()
    console.log();
  },

  quno() {
    this.setData({
      showView: false,
      login_id: '',
    })
  },
  tjok() {
    console.log('++++++++++++++',1);
    if (this.data.flag) {

      this.data.flag = false
      // console.log(111);
      // if (this.data.login_id == '') {
      //   wx.showToast({
      //     title: '请填写支付宝账号',
      //     icon: 'none',
      //     duration: 2000
      //   })
      // } else {
  
        console.log(this.data.USER_COUPON_ID);
        console.log(this.data.login_id);
        this.setData({
          openid: app.globalData.openid
        });
        console.log(this.data.openid);

        wx.request({
          url: app.globalData.interface+'/shanyouhui/API_1023_sendCouponStateALiPay',
          method: 'POST',
          data: {
            USER_COUPON_PHONE: this.data.USER_COUPON_PHONE,
            // 变量
            // USER_COUPON_ID:this.data.USER_COUPON_ID,
            // openid: this.data.openid,
            USER_COUPON_ID: this.data.USER_COUPON_ID,
            // login_id: this.data.login_id,
            KEY:'xianjgjKey9999',
            OPEN_ID: this.data.OPEN_ID, //用户OPEN_ID
            session_key: this.data.session_key,
            OPEN_UUID: this.data.jiami,


          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res.data.userCoupon);
            console.log(res.data.result);
            this.setData({
              // NEW_VAILD:res.data.NEW_VAILD,  
              VAILD: res.data.NEW_VAILD,
            })
            console.log(this.data.NEW_VAILD);
            if (res.data.result != 'success') {
              wx.showToast({
                // title: '领取失败',
                title: res.data.data,
                icon: 'none',
                duration: 1800
              })
              this.setData({
                showView: false,
                login_id: '',
              })

            } else {
              wx.showModal({
                title: '温馨提示',
                // content: '领取成功请前往支付宝卡包查看',
                content: res.data.data,
                success: function (res) {
                  if (res.confirm) {//这里是点击了确定以后
                    console.log('用户点击确定')
                  } else {//这里是点击了取消以后
                    console.log('用户点击取消')
                  }
                }
              })
              this.setData({
                showView: false,
                login_id: '',
              })
            }
          }
        })
        const pages = getCurrentPages()
        const perpage = pages[pages.length - 2]
        perpage.onShow()
        this.onShow() 
      // }
      setTimeout(() => {
        console.log('-----------------',1);
        this.data.flag = true
      }, 5000)
    


    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log('==========', e);
    var daijin = JSON.parse(e.data)
    console.log(daijin);
    this.setData({
      daijin: daijin,
      USER_COUPON_PHONE: daijin.USER_COUPON_PHONE,//抽奖人电话
      VAILD: daijin.VALID,
      USER_COUPON_ID: daijin.USER_COUPON_ID,
      COUPON_IMG: daijin.COUPON_IMG,
      COUPON_BIG_IMG: daijin.COUPON_BIG_IMG,

        jiami: app.globalData.jiami,
        OPEN_ID: app.globalData.openid,
        session_key: app.globalData.session_key,

      // COUPON_BIG_IMG
    })
    console.log(this.data.jiami);
    console.log(this.data.daijin);
    console.log(this.data.VALID);
    console.log(this.data.USER_COUPON_PHONE);
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.receive_a()
    if (this.data.openid == false) {
      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) {  //wx.login获取code。
            console.log(res.code);
            //发起网络请求
            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code  //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                console.log(res.data.openid);
                let openid = res.data.openid;
                app.globalData.openid = openid;
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})